package com.meilai.project.controller.web.adMake;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.constant.AdMakeDeliverTypeEnum;
import com.meilai.project.controller.web.adMake.dto.*;
import com.meilai.project.controller.web.adMake.service.BillAdMakeService;
import com.meilai.project.controller.web.adMake.vo.BillAdMakeConfirmWebVO;
import com.meilai.project.controller.web.adMake.vo.BillAdMakePointFromMarketVO;
import com.meilai.project.controller.web.adMake.vo.BillAdMakeWebVO;
import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.dto.base.SimpleObj;
import com.meilai.project.interceptor.NoAuth;
import com.meilai.project.util.ThreadLocalUserUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author yxg
 * @Date 2022/8/3 0:50
 */
@RestController
@RequestMapping("api/web/bill_ad_make")
@Api(tags = "制作单")
public class WebBillAdMakeController {

    @Autowired
    private BillAdMakeService service;

    @GetMapping("points-from-market")
    @ApiOperation(value = "根据市场单id，查询点位数据")
    public ApiResult<BillAdMakePointFromMarketVO> pointsFromMarket(
            @ApiParam("market_id") @RequestParam(value = "market_id") Long market_id
    ) {
        return ApiResult.ok(service.getPointsByMarketId(market_id));
    }

    @GetMapping("list-media-type-spec")
    @ApiOperation(value = "规格列表")
    public ApiResult<List<JSONObject>> listMediaTypeSpec(
            @ApiParam("media_type_id") @RequestParam(value = "media_type_id") Long media_type_id
    ) {
        return ApiResult.ok(service.listMediaTypeSpec(media_type_id));
    }

    @GetMapping("list-media-pic-type")
    @ApiOperation(value = "材质列表")
    public ApiResult<List<JSONObject>> listMediaPicType(
            @ApiParam("media_type_id") @RequestParam(value = "media_type_id", required = false) Long media_type_id,
            @ApiParam("size_width") @RequestParam(value = "size_width", required = false) String size_width,
            @ApiParam("size_height") @RequestParam(value = "size_height", required = false) String size_height
    ) {
        return ApiResult.ok(service.listMediaPicType(media_type_id, size_width, size_height));
    }

    @GetMapping("list")
    @ApiOperation(value = "列表分页")
    public ApiResult<Page<BillAdMakeWebVO>> list(@ApiParam("form") BillAdMakeSearchParams params) {
        params.setCurrentUserId(ThreadLocalUserUtil.get().getId());
        Page<BillAdMakeWebVO> vo = service.selectPageList(params);
        return ApiResult.ok(vo);
    }

    @ApiOperation(value = "导出供应商清单Excel", notes = "导出供应商清单Excel")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("/exportSupplierDataExcel")
    public void exportSupplierDataExcel(@ApiParam("form") BillAdMakeSearchParams params, HttpServletResponse response) {
        service.exportSupplierDataExcel(response, params);
    }

    @GetMapping("confirm-list")
    @ApiOperation(value = "列表页-生成链接，弹框数据")
    public ApiResult<BillAdMakeWebVO> supplierConfirmLinkData(
            @ApiParam("ad_make_id") @RequestParam(value = "ad_make_id") Long ad_make_id
    ) {
        return ApiResult.ok(service.supplierConfirmLinkData(ad_make_id));
    }

    @GetMapping("detail")
    @ApiOperation(value = "单据明细")
    public ApiResult<BillAdMakeWebVO> detail(@ApiParam("id") @RequestParam(value = "id", required = false) Long id) {
        return ApiResult.ok(service.getOneById(id));
    }

    @GetMapping("get-deliver-type")
    @ApiOperation(value = "获取配送方式")
    public ApiResult<List<SimpleObj>> getDeliverType(
            @ApiParam("isSample, 0：普通配送方式，1：小样配送方式")
            @RequestParam(value = "isSample", required = false) Integer isSample) {
        return ApiResult.ok(AdMakeDeliverTypeEnum.getList(isSample==1));
    }

    @PostMapping("save")
    @ApiOperation("保存")
    public ApiResult<Boolean> save(@ApiParam("json") @RequestBody BillAdMakeCreateParams params) {
        try {
            if (service.save(params)) return ApiResult.ok("新增成功");
            else return ApiResult.err("新增失败");
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("begin")
    @ApiOperation("提交")
    public ApiResult<Boolean> begin(@ApiParam("json") @RequestBody BillAdMakeCreateParams params) {
        try {
            if (service.begin(params)) return ApiResult.ok("提交成功");
            else return ApiResult.err("提交失败");
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("pass")
    @ApiOperation("通过")
    public ApiResult<Boolean> pass(@ApiParam("json") @RequestBody BillAdMakeCheckParams params) {
        try {
            if (service.pass(params)) return ApiResult.ok("通过成功");
            else return ApiResult.err("通过失败");
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("return_back")
    @ApiOperation("驳回")
    public ApiResult<Boolean> returnBack(@ApiParam("json") @RequestBody BillAdMakeCheckParams params) {
        try {
            if (service.returnBack(params)) return ApiResult.ok("驳回成功");
            else return ApiResult.err("驳回失败");
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "逻辑删除")
    public ApiResult<Boolean> delete(@RequestParam("id") @ApiParam("id") Long id) {
        try {
            if (service.delete(id)) return ApiResult.ok("删除成功");
            else return ApiResult.err("删除失败");
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @DeleteMapping("actual_delete")
    @ApiOperation(value = "物理删除")
    public ApiResult<Boolean> actualDelete(@RequestParam("id") @ApiParam("id") Long id) {
        try {
            if (service.actualDelete(id)) return ApiResult.ok("彻底删除成功");
            else return ApiResult.err("彻底删除失败");
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("generate-confirm-link")
    @ApiOperation(value = "生成确认链接(返回批次号batchStr)")
    public ApiResult<String> generateConfirmLink(@RequestBody BillAdMakeGenerateParams params) {
        String batchStr = service.generateConfirmLink(params);
        return ApiResult.ok(batchStr);
    }

    @GetMapping("get-confirm-link")
    @ApiOperation(value = "获取已生成的确认链接(返回批次号batchStr)")
    public ApiResult<String> getConfirmLink(BillAdMakeGenerateParams params) {
        String batchStr = service.getConfirmLink(params);
        return ApiResult.ok(batchStr);
    }

    @NoAuth
    @GetMapping("confirm-bill-detail")
    @ApiOperation(value = "供应商确认单详情（无需登录）")
    public ApiResult<BillAdMakeConfirmWebVO> confirmBillDetail(@RequestParam(value = "batchStr") String batchStr) {
        return ApiResult.ok(service.confirmBillDetail(batchStr));
    }

    @NoAuth
    @PutMapping("sign-confirm-bill")
    @ApiOperation(value = "确认单签字（无需登录）")
    public ApiResult<BillAdMakeConfirmResult> signConfirmBill(@RequestBody BillAdMakeSignParams params) {
        return ApiResult.ok(service.signConfirmBill(params));
    }
}
